/**
 * 投注记录页面
 */
var betLogUI = function() {
    var self = this;
    PageList.call(self);
    
    self.gameSelect = null;
    self.startInput = null;
    self.endInput = null;

    self.startIssueInput = null;
    self.endIssueInput = null;
    self.ipInput = null;
    self.userInput = null;

    self.stateSelect = null;
    self.table = null;
    self.phone = false;
    
    // 今日、昨日选择
    self.dataSelect = null;
    self.startEnd = null;

    // 查找按钮
    self.queryBtn = null;

    // 自动刷新
    self.autoChecked = null;
    self.autoTime = 8 * 1000;

    self.admin = true;
};
betLogUI.prototype = Object.create(PageList.prototype);

betLogUI.prototype.init = function() {
    var self = this;

    // 填充游戏列表
    var f = function() {
        self.currPage = 1; 
        self.totalPage = -1;
        self.query(1);
    };
    if (self.gameSelect) {
        var arr = get_game_list();
        self.gameSelect.append('<option value="-1">所有彩种</option>');
        for (var i in arr) {
            var line = '<option value="__v__">__name__</option>';
            line = line.replace(/__v__/, arr[i]);
            line = line.replace(/__name__/, get_game_display_name(arr[i]));
            self.gameSelect.append(line);
        }

        var game = urlInfo.params.game;
        if (game) {
            self.gameSelect.val(game);
            self.gameSelect.trigger('changed.selected.amui');
        }
        self.gameSelect.change(f);
    }
    
    if (self.stateSelect) self.stateSelect.change(f);
    if (self.startInput) self.startInput.change(f);
    if (self.endInput) self.endInput.change(f);
    if (self.dataSelect) self.dataSelect.change(f);

    if (self.startInput) self.startInput.datepicker({format: 'yyyy-mm-dd'});
    if (self.endInput) self.endInput.datepicker({format: 'yyyy-mm-dd'});

    // 立刻检索下数据
    if (window.ME)
        ME.query(function() {
            f();
        });
    else f();

    if (self.autoChecked) {
        self.autoChecked.change(function() {
            if (self.autoChecked[0].checked) {
                self.timer = setTimeout(function() {
                    f();
                }, self.autoTime);
            }
            else {
                if (self.timer) clearTimeout(self.timer);
            }
        });
    }

    self.jumpPage = function(page) {
        self.query(page);
    };

    if (self.queryBtn) self.queryBtn.click(f);
}

betLogUI.prototype.query = function(page) {
    var self = this;
    if (self.timer) {
        clearTimeout(self.timer);
        self.timer = null;
    }
    
    var type = null;
    if (self.gameSelect) {
        type = self.gameSelect.val();
        if (type == '-1') type = null;
    }

    var start = null, end = null;
    if (self.startEnd) {
        start = self.startEnd;
        end  = self.startEnd;
    }
    if (self.startInput) {
        start = self.startInput.val().trim();
    }
    if (self.endInput) {
        end = self.endInput.val().trim();
    }
    if (self.dataSelect) {
        var start = new Date(get_server_time()*1000).Format("yyyy-MM-dd");
        if ($(".f2 select").val() == "1") start = new Date(get_server_time()*1000 - 24 * 3600 * 1000).Format("yyyy-MM-dd");
        end = start;
    }

    var state = -1;
    if (self.stateSelect) state = self.stateSelect.val();

    var uid = null;
    if (self.userInput) uid = self.userInput.val().trim();

    var startIssue = null, endIssue = null;
    if (self.startIssueInput) startIssue = self.startIssueInput.val();
    if (self.endIssueInput) endIssue = self.endIssueInput.val();

    var ip = null;
    if (self.ipInput) ip = self.ipInput.val();

    // 开始检索
    var data = {
        pageSize: self.getPageSize(),
        pageNum: page || 1,
    }
    if (uid) data.uid = uid;
    if (ip) data.ip = ip;
    if (start) data.start = start;
    if (end) data.end = end;
    if (startIssue) data.startIssue = startIssue;
    if (endIssue) data.endIssue = endIssue;
    if (data.startIssue && data.endIssue) data.issues = data.startIssue + "," + data.endIssue;
    else if (data.startIssue) data.issues = data.startIssue + ",";
    else if (data.endIssue) data.issues = "," + data.endIssue;
    
    if (state != -1)
        data.state = state;
    startLoading();
    if (self.admin)
        CMD.query_bet(type, "", data, function(data) {
            stopLoading();
            self.totalPage = data.page;
            self.fillResult(data.list, data.sum);
            self.fillPages(data.count, data.number, data.page, data.size);
        });
    else
        CMD.query_bet_by_user(type, data, function(data) {
                stopLoading();
                self.totalPage = data.page;
                self.fillResult(data.list, data.sum);
                self.fillPages(data.count, data.number, data.page, data.size);
            });

    if (self.autoChecked) {
        if (self.autoChecked[0].checked) {
            console.log("延迟8s刷新。");
            self.timer = setTimeout(function() {
                self.query(self.currPage);
            }, self.autoTime);
        }
    }
}

betLogUI.prototype.fillResult = function(list, sum) {
    var self = this;
    if (!self.table) return;

    self.table.empty();
    var ids = [];
    if (!Util.isEmpty(sum)) {
        if (!self.phone && self.admin) {
            var tr = $("<tr></tr>");
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            tr.append("<td>合计</td>");
            tr.append("<td>" + (sum.amount/100).toFixed(2) + "</td>");
            tr.append("<td>" + + (sum.profit/100).toFixed(2) + "</td>");
            tr.append("<td>--</td>");
            self.table.append(tr);
        }
    }

    for (var i in list) {
        var o = list[i];
        var tr = $("<tr></tr>");
        if (self.customDraw) {
            self.customDraw(o, tr);
        }
        else if (!self.phone) {
            tr.append("<td>" + Util.converServerTime(o.time).substring(5) + "</td>");
            tr.append("<td>" + get_game_display_name(o.type) + " 第" + o.issue + "期</td>");
            tr.append("<td>" + Util.convertTarget(o.type, o.target) + "@" + o.rate + "</td>");
            tr.append("<td>" + (o.amount/100) + "</td>");
            tr.append("<td>" + (o.back || 0) + "%</td>");

            if (o.state == 0) {
                tr.append("<td>未结</td>");
            }
            else {
                if (o.profit > 0)
                    tr.append("<td class='red'>" + (o.profit/100).toFixed(3) + "</td>");
                else
                    tr.append("<td class='green'>" + (o.profit/100).toFixed(3) + "</td>");
            }
        }
        else {
            var c = "";
            c += "<div class='game'>" + get_game_display_name(o.type) + " " + o.issue + "期</div>";
            c += "<div class='time'>" + Util.converServerTime(o.time) + "</div>";
            tr.append("<td>" + c + "</td>");

            tr.append("<td>" + Util.convertTarget(o.type, o.target) + "<br/>" + o.rate + "</td>");
            tr.append("<td>" + (o.amount/100).toFixed(0) + "</td>");
            if (o.profit >= 0)
                tr.append("<td class='red'>" + (o.profit/100).toFixed(2).toFixed(3) + "</td>");
            else
                tr.append("<td class='green'>" + (o.profit/100).toFixed(2).toFixed(3) + "</td>");
        }
        
        self.table.append(tr);
    }
    self.registerIps();
}

betLogUI.prototype.defaultDraw = function(o, tr) {
    var self = this;
    var target = Util.convertTarget(o.type, o.target);
    tr.append("<td>" + Util.converServerTime(o.time).substring(5) + "</td>");
    tr.append("<td>" + Util.converServerTime(o.complete).substring(5) + "</td>");
    tr.append("<td>" + get_game_display_name(o.type) + " 第" + o.issue + "期</td>");
    tr.append("<td>" + (o.user.mobile || "游客") + "</td>");
    tr.append("<td class='ip'>" + o.ip + "</td>");
    tr.append("<td>" + target + "@" + o.rate + "</td>");
    tr.append("<td>" + (o.amount/100) + "</td>");

    if (o.profit > 0)
        tr.append("<td class='red'>" + (o.profit/100).toFixed(3) + "</td>");
    else
        tr.append("<td class='green'>" + (o.profit/100).toFixed(3) + "</td>");
        
    if (self.admin)
        tr.append("<td>" + (o.source || '') + '</td>');
}

betLogUI.prototype.registerIps = function() {
    var arr = $("td.ip");
    var ips = [];
    for (var i = 0; i < arr.length; i++) {
        var ip = $(arr[i]).html();
        if (ip && ips.indexOf(ip) == -1) ips.push(ip);
    }

    for (var i in ips)
        this.registerIp(ips[i]);
}
betLogUI.prototype.registerIp = function(ip) {
    CMD.ip_info(ip, function(r) {
        var arr = $("td.ip");
        for (var i = 0; i < arr.length; i++) {
            if ($(arr[i]).html() != ip) continue;
            $(arr[i]).html(ip + "(" + r.addr + ")");
        }
    });
}

window.BetLogUI = new betLogUI();